<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class ControlCategoriesBase {
	var $feObj = NULL;
	var $modelObj = NULL;
	var $vObj = NULL;
	
	var $actionHandler = NULL;
	var $displayHandler = NULL;
	
	//contructor
	function ControlCategoriesBase($config = '') {
		if( is_array($config) ) {
			foreach($config as $k => $v) {
				$this->$k = $v;
			}
		}
		
		//init internal
		$this->_init();
	}
	
	function _init() {
		//model
		if( $this->class_file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $this->class_file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $this->class_file);
		}
		$this->modelObj = new $this->class_name();
		$this->modelObj->format = false;
		
		//view
		global $feObj;
		$this->feObj = $feObj;
		
		//validate
		//$this->vObj = new CommonValidate();
		global $vObj;
		$this->vObj = $vObj;
		
		//inject
		$this->_init_inject();
		
		//filter
		$this->_init_filter();
	}
	
	function _init_inject() {
		if( isset($this->inject) && is_array($this->inject) ) {
			foreach($this->inject as $k => $v) {
				if( is_array($v) ) {
					if( $v['type'] == 'cm' ) {
						if( $v['class_file'] != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $v['class_file']) ) {
							include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $v['class_file']);
						}
						$cmObj = new $v['class_name']();
						//$vs = $cmObj->$v['method']();
						$vs = call_user_func_array(array($cmObj, $v['method']), $v['params']);
						$this->feObj->addInject($k, $vs);
					}
				}
			}
		}
	}
	
	function _init_filter() {
		$this->filter['add'] = array('check' => 'field_image', 'fields' => '^field_image');
		$this->filter['update'] = array('check' => 'field_image', 'fields' => '^field_image');
	}
	
	//init
	function init() {
		
	}
	
	//process
	function process() {
		$this->_doBefore();
		$this->_doAction();
		$this->_doDisplay();
		$this->_doAfter();
	}
	
	//methods
	function _doBefore() {
		
	}
	
	function _doAfter() {
		
	}
	
	function _doAction() {
		//action
		if( $this->_check_action('add') ) {
			$this->vObj->checkNull($_POST, array('name'));
			if( $this->vObj->_valid() ) {
				$data_array = $this->_doFilter($_POST, 'add');
				$data_array = $this->modelObj->_post_field($data_array, 'categories');
				$this->modelObj->addCategory($data_array);
				//
				$this->feObj->redirect($this->feObj->request_link_current, 's');
			} else {
				$GLOBALS['_SUBMITTED_'] = 1;
				
				$vm = $this->vObj->_result("Error");
				$this->feObj->addInject('vm', $vm);
			}
		} elseif( $this->_check_action('update') ) {
			$data_array = array();
			$id = trim(strtolower(util_get_param($_POST, 'cid')));
			if( $id > 0 ) {
				$this->vObj->checkNull($_POST, array('name'));
				if( $this->vObj->_valid() ) {
					$data_array = $this->_doFilter($_POST, 'update');
					$data_array = $this->modelObj->_post_field($data_array, 'categories');
					$this->modelObj->updateCategory($id, $data_array);
					//
					$this->feObj->redirect($this->feObj->request_link_current, 's');
				} else {
					$GLOBALS['_SUBMITTED_'] = 1;
					
					$vm = $this->vObj->_result("Error");
					$this->feObj->addInject('vm', $vm);
				}
			} else {
				$this->feObj->redirect($this->feObj->request_link_current, 's');
			}
		} elseif( $this->_check_action('delete') ) {
			$id = trim(strtolower(util_get_param($_POST, 'cid')));
			if( $id > 0 ) {
				$this->modelObj->deleteCategory($id);
			}
			//
			$this->feObj->redirect($this->feObj->request_link_current, 's');
		} elseif( $this->_check_action('dodelete') || $this->_check_request('dodelete') ) {
			$id = trim(strtolower(util_get_param($_GET, 'cid')));
			if( $id > 0 ) {
				$this->modelObj->deleteCategory($id);
			}
			//
			$this->feObj->redirect($this->feObj->request_link_current, 's');
		} elseif( $this->_check_action('delete%ss') ) {
			$ids = util_get_param($_POST, 'cb');
			if( is_array($ids) ) {
				$this->modelObj->deleteCategories($ids);
			}
			//
			$this->feObj->redirect($this->feObj->request_link_current, 's');
		} elseif( $this->_check_action('toadd') ) {
			$this->feObj->redirect($this->feObj->request_link_current . $this->_make_request('add') . '/');
		} elseif( $this->_checkActionHandler() == true ) {
			$this->_doActionHandler();
		}
	}
	
	function _checkActionHandler() {
		return (!empty($this->actionHandler) ? true : false);
	}
	
	function _doActionHandler() {
		$f = $this->actionHandler;
		if( is_array($f) && sizeof($f) >= 2 ) {
			if( $f['0'] == 'this' ) {
				call_user_func_array(array($this, $f['1']), $this);
			} elseif( is_object($f['0']) ) {
				call_user_func_array(array($f['0'], $f['1']), $this);
			}
		} elseif( function_exists($f) ) {
			call_user_func_array($f, $this);
		}
		
		return false;
	}
	
	function _doDisplay() {
		$is_default = false;
		
		if( $this->_check_request('add') ) {
			$this->feObj->content_wrap['mainpart'] = $this->_make_request('add');
		} elseif( $this->_check_request('view') || $this->_check_request('edit') ) {
			$mainpart = $this->_make_request($this->feObj->operation);
			$id = $this->feObj->_request['cid'];
			if( $id > 0 ) {
				$_valid = $this->modelObj->checkCategory($id);
				if( $_valid == true ) {
					$filter = array('parent' => 0);
					$categories = $this->modelObj->getCategoriesTree($filter, 0, false);
					$category = $this->modelObj->getCategory($id);
					$category['categories_subs'] = $this->modelObj->getCategoriesSubsIDs($id, '', -1, false);
					
					$this->feObj->content_wrap = array(
								'mainpart' => $mainpart,
								'categories' => $categories,
								'category' => $category,
										);
				} else {
					$this->feObj->content_wrap['mainpart'] = $this->_make_request('no');
				}
			} else {
				$is_default = true;
			}
		} elseif( $this->_checkDisplayHandler() == true ) {
			$this->_doDisplayHandler();
		} else {
			$is_default = true;
		}
		
		//
		if( $is_default == true ) {
			$filter = array('parent' => 0);
			$categories = $this->modelObj->getCategoriesTree($filter, 0, false);
			
			$this->feObj->content_wrap['categories'] = $categories;
		}
		
		//
		echo $this->feObj->display($this->tpl_file);
	}
	
	function _checkDisplayHandler() {
		return (!empty($this->displayHandler) ? true : false);
	}
	
	function _doDisplayHandler() {
		$f = $this->displayHandler;
		if( function_exists($f) ) {
			call_user_func_array($f, $this);
		}
		
		return false;
	}	
	
	function _doFilter($data_array, $type) {
		if( isset($this->filter[$type]) ) {
			$check = $this->filter[$type]['check'];
			$fields = $this->filter[$type]['fields'];
			if( !is_array($check) ) {
				$check = array($check);
			}
			if( !is_array($fields) ) {
				$fields = array($fields);
			}
			$pass = true;
			foreach($check as $k => $v) {
				if( isset($data_array[$v]) && strlen($data_array[$v]) == 0 ) {
					$pass = false;
				}
			}
			if( $pass == false ) {
				foreach($data_array as $dk => $dv) {
					foreach($fields as $k => $v) {
						if( ereg($v, $dk) == true ) {
							unset($data_array[$dk]);
						}
					}
				}
			}
		}
		
		return $data_array;
	}
	
	//
	function _check_action($action) {
		if( in_array($this->feObj->action, array($action, $action . $this->cname, sprintf($action, $this->cname))) ) {
			return true;
		}
		
		return false;
	}
	
	function _make_action($action) {
		return $action . $this->cname;
	}
	
	function _check_request($request) {
		$_r = $this->feObj->operation;
		if( in_array($_r, array($request, $request . $this->cname, sprintf($request, $this->cname))) ) {
			return true;
		}
		
		return false;
	}
	
	function _make_request($request) {
		return $request . $this->cname;
	}
	
	//DISPLAY
	//
	function display_default() {
		
	}
	
	function display_category() {
		
	}
	
	function display_category_detail() {
		
	}
	
	function display_category_add() {
		$this->feObj->content_wrap['mainpart'] = $this->_make_request('add');
	}
	
	function display_category_edit() {
		$mainpart = $this->_make_request($this->feObj->operation);
		$id = $this->feObj->_request['cid'];
		if( $id > 0 ) {
			$_valid = $this->modelObj->checkCategory($id);
			if( $_valid == true ) {
				$filter = array('parent' => 0);
				$categories = $this->modelObj->getCategories($filter, 0, false);
				$category = $this->modelObj->getCategory($id);
				
				$content_wrap = array(
							'mainpart' => $mainpart,
							'categories' => $categories,
							'category' => $category,
									);
			} else {
				$content_wrap = array(
							'mainpart' => $this->_make_request('no'),
								);
			}
		} else {
			$is_default = true;
		}
	}
	
	function display_category_delete() {
		
	}
	
	function display_categories() {
		
	}
	
	function display_categories_detail() {
		
	}
	
	function display_categories_search() {
		
	}
	
	//ACTION
	//
	function do_action_default() {
		
	}
	
	function do_action_add() {
		$this->vObj->checkNull($_POST, array('name'));
		if( $this->vObj->_valid() ) {
			$data_array = $this->_doFilter($_POST, 'add');
			$data_array = $this->modelObj->_post_field($data_array, 'categories');
			$this->modelObj->addCategory($data_array);
			//
			$this->feObj->redirect($this->feObj->request_link_current, 's');
		} else {
			$GLOBALS['_SUBMITTED_'] = 1;
			
			$vm = $this->vObj->_result("Error");
			$this->feObj->addInject('vm', $vm);
		}
	}
	
	function do_action_update() {
		$data_array = array();
		$id = trim(strtolower(util_get_param($_POST, 'cid')));
		if( $id > 0 ) {
			$this->vObj->checkNull($_POST, array('name'));
			if( $this->vObj->_valid() ) {
				$data_array = $this->_doFilter($_POST, 'update');
				$data_array = $this->modelObj->_post_field($data_array, 'categories');
				$this->modelObj->updateCategory($id, $data_array);
				//
				$this->feObj->redirect($this->feObj->request_link_current, 's');
			} else {
				$GLOBALS['_SUBMITTED_'] = 1;
				
				$vm = $this->vObj->_result("Error");
				$this->feObj->addInject('vm', $vm);
			}
		} else {
			$this->feObj->redirect($this->feObj->request_link_current, 's');
		}
	}
	
	function do_action_delete() {
		$id = trim(strtolower(util_get_param($_POST, 'cid')));
		if( $id > 0 ) {
			$this->modelObj->deleteCategory($id);
		}
		//
		$this->feObj->redirect($this->feObj->request_link_current, 's');
	}
	
	function do_action_deletes() {
		
	}
	
	function do_action_setstatusopen() {
		
	}
	
	function do_action_setstatusclose() {
		
	}
}
//
?>